<?php
/**
 * Created by PhpStorm.
 * Author: ls
 * Time: 2024/1/10 11:03
 * Desc:  订单支付完成处理-（处理父子订单）
 */
class Listener_Order_Payed_Order extends Listener_Base
{
    public function handle($args)
    {
        $self       = new self();
        $self->args = $args;
        // 目前只处理景点订单，后续根据需求再扩展
        if ($args['typeid'] == 5 && $args['haschild'] == 1) {
            $self->perform();
        }
    }

    public function perform()
    {
        $order      = $this->args;
        $order_id   = $order['id'];
        $order_sn   = $order['ordersn'];
        $pay_source = $order['pay_source'] ?? '';

        $data = array(
            'ispay' => 1,
            'paytime' => $order['paytime'],
            'online_transaction_no' => $order['online_transaction_no'],
            'status' => 2,
            'paysource' => $pay_source,
        );

        $children = DB::select('id', 'ordersn')
            ->from('member_order')
            ->where('pid', '=', $order_id)
            ->execute()
            ->as_array();

        // 处理子订单
        DB::update('member_order')
            ->set($data)
            ->where('pid', '=', $order_id)
            ->execute();

        foreach ($children as $child) {
            // 给子订单生成消费码
            St_Product::add_eticketno($child['ordersn']);

            $child_order = array_merge($child, $data);

            //订单状态变更事件
            Hook::trigger('order_status_changed', array('order' => $child_order));
        }
    }
}